package com.myway.dto.common;

import java.io.Serializable;

/**
 * Created by zhu on 2017/7/5.
 */
public class ResultEntity implements Serializable{

    //业务响应状态。0:异常/操作失败/无返回数据，1:正常/操作成功/有返回数据
    private Integer status;

    //业务响应消息。错误提示或正确提示
    private String message;

    //业务响应数据。
    private Object data;

    //请求响应状态。HTTP状态码，默认200，即请求成功。-1表示登录已过期。-2表示用户未认证。-3表示无权限。
    private Integer code;

    //额外数据。
    //适用于。 例：当data中返回List时，同时需要返回一个单独的字段值，此时便可放在extra中。
    //             可避免因为多返回了一个单独字段而修改data返回的结构，减少后端、前端操作步骤。
    private Object extra;



    public ResultEntity() {
    }


    public static ResultEntity build(Integer status, String message) {
        return build(status, message, new Object());
    }

    public static ResultEntity build(Integer status, String message, Object data) {
        return build(status, message, data, 200);
    }

    public static ResultEntity build(Integer status, String message, Object data, Integer code) {
        return build(status, message, data, code, "");
    }

    public static ResultEntity build(Integer status, String message, Object data, Integer code, Object extra) {
        return new ResultEntity(status, message, data, code, extra);
    }



    public ResultEntity(int status, String message) {
        this(status, message, new Object());
    }

    public ResultEntity(int status, String message, Object data) {
        this(status, message, data, 200);
    }

    public ResultEntity(int status, String message, Object data, int code) {
        this(status, message, data, code, "");
    }

    public ResultEntity(int status, String message, Object data, int code, Object extra) {
        this.status = status;
        this.message = message;
        this.data = data;
        this.code = code;
        this.extra = extra;
    }


    public static ResultEntity success(String message) {
        return success(message, "");
    }

    public static ResultEntity success(String message, Object data) {
        return success(message, data, "");
    }

    public static ResultEntity success(String message, Object data, Object extra) {
        return new ResultEntity(1, message, data, 200, extra);
    }


    public static ResultEntity fail(String message) {
        return fail(message, 200);
    }

    public static ResultEntity fail(String message, Integer code) {
        return fail(message, code, "");
    }

    public static ResultEntity fail(String message, Integer code, Object extra) {
        return new ResultEntity(0, message, "", code, extra);
    }

    public static ResultEntity notLogin() {
        return new ResultEntity(0, "请先登录", "", -1);
    }

    public static ResultEntity errorParam() {
        return new ResultEntity(0, "参数错误", "");
    }

    public static ResultEntity noCertify() {
        return new ResultEntity(0, "请用户认证后再进行此操作", "", -2);
    }

    public static ResultEntity noPermit() {
        return new ResultEntity(0, "没有权限进行此操作", "", -3);
    }


    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public Object getExtra() {
        return extra;
    }

    public void setExtra(Object extra) {
        this.extra = extra;
    }

    @Override
    public String toString() {
        return "ResultEntity{" +
                "status=" + status +
                ", code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data + '\'' +
                ", extra=" + extra +
                '}';
    }
}
